﻿/*
=======================================================
    StockWireropeKeep【备绳维保】
=======================================================
*/

namespace Services
{
    public interface IStockWireropeKeepMapping : IMapping<StockWireropeKeep, StockWireropeKeepModel, StockWireropeKeepFilterModel>
    { }

    public class StockWireropeKeepMapping : AutoStockWireropeKeepMapping, IStockWireropeKeepMapping
    {
        private readonly ISqlSugarClient _DB;

        public StockWireropeKeepMapping(ICurrentUser currentUser, ISqlSugarClient dB)
            : base(currentUser)
        {
            _DB = dB;
        }

        #region 读数据
        /// <summary>
        /// 读数据，从Entity对象转成Model对象
        /// </summary>
        /// <param name="entity">Entity对象</param>
        /// <returns></returns>
        public override StockWireropeKeepModel Entity2Model(StockWireropeKeep entity)
        {
            if (entity == null)
            {
                return null;
            }

            StockWireropeKeepModel model = base.Entity2Model(entity);


            /***************************************** 下面处理自定义转换 *****************************************/



            return model;
        }
        #endregion

        #region 写数据
        /// <summary>
        /// 写数据，从Model对象转换成Entity对象
        /// </summary>
        /// <param name="model">ViewModel对象</param>
        /// <returns></returns>
        public override StockWireropeKeep Model2Entity(StockWireropeKeepModel model)
        {
            StockWireropeKeep entity = base.Model2Entity(model);


            /***************************************** 下面处理自定义转换 *****************************************/



            return entity;
        }
        #endregion

        #region 读数据(集合)
        /// <summary>
        /// 读数据，从Entity对象转成Model对象
        /// </summary>
        /// <param name="entities">Entity对象</param>
        /// <returns></returns>
        public override List<StockWireropeKeepModel> Entity2Model(IEnumerable<StockWireropeKeep> entities)
        {
            return entities.Select(x => Entity2Model(x)).ToList();
        }
        #endregion

        #region 写数据(集合)
        /// <summary>
        /// 写数据，从Model对象转换成Entity对象
        /// </summary>
        /// <param name="models">ViewModel对象</param>
        /// <returns></returns>
        public override List<StockWireropeKeep> Model2Entity(IEnumerable<StockWireropeKeepModel> models)
        {
            return models.Select(x => Model2Entity(x)).ToList();
        }
        #endregion

        #region 搜索条件转换
        /// <summary>
        /// 搜索条件转换，从FilterModel对象转换成Where对象
        /// </summary>
        /// <param name="filterModel">FilterModel对象</param>
        /// <returns></returns>
        public override Expressionable<StockWireropeKeep> FilterModel2Where(StockWireropeKeepFilterModel filterModel)
        {
            var where = base.FilterModel2Where(filterModel);

            //处理数据权限
            if (_currentUser.CurrentUserInfo.LoginUserType == LoginUserType.集团)
            {
                var stockIDs = _DB.Queryable<Stock>().Where(x => _currentUser.CurrentUserInfo.LoginUserWarehouseIDsWithDataDepartment.Contains(x.WarehouseID))
                    .Select(x => x.StockID).Distinct().ToList();

                where = where.And(x => stockIDs.Contains(x.StockID));
            }
            else if (_currentUser.CurrentUserInfo.LoginUserType == LoginUserType.监管部门)
            {
                var stockIDs = _DB.Queryable<Stock>().Where(x => x.DepartmentID.HasValue && _currentUser.CurrentUserInfo.LoginRegulatorDepartmentIDs.Contains(x.DepartmentID.Value))
                            .Select(x => x.StockID).Distinct().ToList();

                where = where.And(x => stockIDs.Contains(x.StockID));
            }

            //SearchInput
            if (!string.IsNullOrEmpty(filterModel._SearchInput))
            { }

            ////操作人
            //if (filterModel._OperUserID.HasValue)
            //{
            //    where = where.And(x => x.OperUserID == filterModel._OperUserID.Value);
            //}

            //钢丝绳名称
            if (!filterModel._StockName.IsEmpty())
            {
                var _expr = Expressionable.Create<Stock>();
                if (_currentUser.CurrentUserInfo.LoginUserType == LoginUserType.集团)
                {
                    _expr = _expr.And(x => x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID);
                }
                _expr = _expr.And(x => x.Name.Contains(filterModel._StockName));

                var stockIDs = _DB.Queryable<Stock>().Where(_expr.ToExpression()).Select(x => x.StockID).Distinct().ToList();
                where = where.And(x => stockIDs.Contains(x.StockID));
            }

            //规格
            if (!filterModel._StockGuiGe.IsEmpty())
            {
                var _expr = Expressionable.Create<Stock>();
                if (_currentUser.CurrentUserInfo.LoginUserType == LoginUserType.集团)
                {
                    _expr = _expr.And(x => x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID);
                }
                _expr = _expr.And(x => x.GuiGe.Contains(filterModel._StockGuiGe));

                var stockIDs = _DB.Queryable<Stock>().Where(_expr.ToExpression()).Select(x => x.StockID).Distinct().ToList();
                where = where.And(x => stockIDs.Contains(x.StockID));
            }

            //部门
            if (filterModel._StockDepartmentIDs.IsNotEmpty() && filterModel._StockDepartmentIDs.Count > 0)
            {
                var _expr = Expressionable.Create<Stock>();
                if (_currentUser.CurrentUserInfo.LoginUserType == LoginUserType.集团)
                {
                    _expr = _expr.And(x => x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID);
                }
                _expr = _expr.And(x => x.DepartmentID.HasValue && filterModel._StockDepartmentIDs.Contains(x.DepartmentID.Value));

                var stockIDs = _DB.Queryable<Stock>().Where(_expr.ToExpression()).Select(x => x.StockID).Distinct().ToList();
                where = where.And(x => stockIDs.Contains(x.StockID));
            }

            //仓库，只有集团才有
            if (filterModel._StockWarehouseIDs.IsNotEmpty() && filterModel._StockWarehouseIDs.Count > 0 && _currentUser.CurrentUserInfo.LoginUserType == LoginUserType.集团)
            {
                var stockIDs = _DB.Queryable<Stock>().Where(x => x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID && filterModel._StockWarehouseIDs.Contains(x.WarehouseID))
                 .Select(x => x.StockID).Distinct().ToList();

                where = where.And(x => stockIDs.Contains(x.StockID));
            }

            return where;
        }
        #endregion
    }
}
